<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.height()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/css/">CSS</a></span><span class="category-divider"> | </span><span class="category"><a href="/category/dimensions/">尺寸</a></span><span class="category-divider"> | </span><span class="category"><a href="/category/manipulation/">DOM 操作</a> &gt; <a href="/category/manipulation/style-properties/">CSS 属性</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/height/" target="_blank">.height()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
			    获取匹配元素集合中的第一个元素的当前计算高度值
			或
			设置每一个匹配元素的高度值。<div class="toc">
<h4><span>Contents:</span></h4>
<ul class="toc-list">
<li>
<a href="#height1">.height()</a><ul><li>.height()</li></ul>
</li>
<li>
<a href="#height2">.height( value )</a><ul>
<li>.height( value )</li>
<li>.height( function(index, height) )</li>
</ul>
</li>
</ul>
</div><article class="entry method" id="height1"><h2 class="section-title">
<span class="name">.height()</span><span class="returns">返回: <a href="/Types/#Integer">Integer</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>获取匹配元素集合中的第一个元素的当前计算高度值。</p>
<ul class="signatures"><li class="signature" id="height">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.height()</h4>
<ul><li><div class="null-signature">这个方法不接受任何参数。</div></li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p><code>.css('height')</code> 和 <code>.height()</code>之间的区别是后者返回一个没有单位的数值（例如，<code>400</code>），前者是返回带有完整单位的字符串（例如，<code>400px</code>）。当一个元素的高度需要数学计算的时候推荐使用<code>.height()</code> 方法 。</p>
<p class="image">
<img src="/resources/0042_04_01.png"/>
</p>
<p>
			      这个方法同样能计算出window和document的高度。</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(window).height();   <span class="comment">// returns height of browser viewport</span></code></div></div><div class="container"><div class="line"><code>$(document).height(); <span class="comment">// returns height of HTML document</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>注意<code>.height()</code>总是返回内容宽度,不管CSS <code>box-sizing</code>属性值。</p>
<blockquote>
<p><strong>注意：</strong>在绝对定位和给定<code>display:block</code>时，虽然<code>style</code>和<code>script</code>标签用<code>.width()</code> 或 <code>height()</code>也将报告一个值，强烈建议不要在这些标签中调用这些方法。这是一种不好的做法，结果也证明是不可靠的。
			         </p>
</blockquote>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">显示各个高度。注意这个来自值iframe的值可能小于你的预期。黄色高亮显示iframe body。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
<div class="line n28">28</div>
<div class="line n29">29</div>
<div class="line n30">30</div>
<div class="line n31">31</div>
<div class="line n32">32</div>
<div class="line n33">33</div>
<div class="line n34">34</div>
<div class="line n35">35</div>
<div class="line n36">36</div>
<div class="line n37">37</div>
<div class="line n38">38</div>
<div class="line n39">39</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">body</span> <span class="rules">{ <span class="rule"><span class="attribute">background</span>:<span class="value">yellow;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">button</span> <span class="rules">{ <span class="rule"><span class="attribute">font-size</span>:<span class="value"><span class="number">12</span>px;</span></span> <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">2</span>px;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">p</span> <span class="rules">{ <span class="rule"><span class="attribute">width</span>:<span class="value"><span class="number">150</span>px;</span></span> <span class="rule"><span class="attribute">border</span>:<span class="value"><span class="number">1</span>px red solid;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">color</span>:<span class="value">red;</span></span> <span class="rule"><span class="attribute">font-weight</span>:<span class="value">bold;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">button</span> <span class="attribute">id</span>=<span class="value">"getp"</span>&gt;</span>Get Paragraph Height<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">button</span> <span class="attribute">id</span>=<span class="value">"getd"</span>&gt;</span>Get Document Height<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">button</span> <span class="attribute">id</span>=<span class="value">"getw"</span>&gt;</span>Get Window Height<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span>&gt;</span>&amp;nbsp;<span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">p</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>    Sample paragraph to test height</code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;/<span class="title">p</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code>    <span class="function"><span class="keyword">function</span> <span class="title">showHeight</span><span class="params">(ele, h)</span> {</span></code></div></div><div class="container"><div class="line"><code>      $(<span class="string">"div"</span>).text(<span class="string">"The height for the "</span> + ele +</code></div></div><div class="container"><div class="line"><code>                    <span class="string">" is "</span> + h + <span class="string">"px."</span>);</code></div></div><div class="container"><div class="line"><code>    }</code></div></div><div class="container"><div class="line"><code>    $(<span class="string">"#getp"</span>).click(<span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span></code></div></div><div class="container"><div class="line"><code>      showHeight(<span class="string">"paragraph"</span>, $(<span class="string">"p"</span>).height());</code></div></div><div class="container"><div class="line"><code>    });</code></div></div><div class="container"><div class="line"><code>    $(<span class="string">"#getd"</span>).click(<span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span></code></div></div><div class="container"><div class="line"><code>      showHeight(<span class="string">"document"</span>, $(document).height());</code></div></div><div class="container"><div class="line"><code>    });</code></div></div><div class="container"><div class="line"><code>    $(<span class="string">"#getw"</span>).click(<span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span></code></div></div><div class="container"><div class="line"><code>      showHeight(<span class="string">"window"</span>, $(window).height());</code></div></div><div class="container"><div class="line"><code>    });</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article><article class="entry method" id="height2"><h2 class="section-title">
<span class="name">.height( value )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>设置每一个匹配元素的高度值。</p>
<ul class="signatures">
<li class="signature" id="height-value">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.height( value )</h4>
<ul><li>
<div><strong>value</strong></div>
<div>类型: <a href="/Types/#String,%20Number">String, Number</a>
</div>
<div>一个正整数代表的像素数,或是整数和一个可选的附加单位（默认是：“px”）(作为一个字符串)。
			</div>
</li></ul>
</li>
<li class="signature" id="height-functionindex--height">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.4.1/">1.4.1</a></span>.height( function(index, height) )</h4>
<ul><li>
<div><strong>function(index, height)</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>返回用于设置高度的一个函数。该函数接受两个参数，index 参数表示元素在集合中的位置，height 参数表示原来的高度。在这个函数中，<code>this</code>指向元素集合中的当前元素。</div>
</li></ul>
</li>
</ul>
<div class="longdesc" id="entry-longdesc-1">
<p>当调用<code>.height(value)</code>方法的时候，这个“value”参数可以是一个字符串（数字加单位）或者是一个数字。如果这个“value”参数只提供一个数字，jQuery会自动加上单位px。如果只提供一个字符串，任何有效的CSS尺寸都可以为高度赋值（就像<code>100px</code>, <code>50%</code>, 或者 <code>auto</code>）。注意在现代浏览器中，CSS高度属性不包含padding, border, 或者 margin。</p>
<p>如果没有给定明确的单位（像'em' 或者 '%'），那么默认情况下"px"会被直接添加上去（也理解为"px"是默认单位）。</p>
<p>注意<code>.height('value')</code>设置的容器宽度是根据CSS <code>box-sizing</code>属性来定的, 将这个属性值改成<code>border-box</code>，将造成这个函数改变这个容器的outerHeight，而不是原来的内容高度。</p>
</div>
<section class="entry-examples" id="entry-examples-1"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-1-0">
<h4><span class="desc">点击每个div时设置该div高度为30px，并改变颜色。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"><span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">width</span>:<span class="value"><span class="number">50</span>px;</span></span> <span class="rule"><span class="attribute">height</span>:<span class="value"><span class="number">70</span>px;</span></span> <span class="rule"><span class="attribute">float</span>:<span class="value">left;</span></span> <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">5</span>px;</span></span></span></span></code></div></div><div class="container"><div class="line"><code>        <span class="rule"><span class="attribute">background</span>:<span class="value"><span class="function">rgb(<span class="number">255</span>,<span class="number">140</span>,<span class="number">0</span>)</span>;</span></span> <span class="rule"><span class="attribute">cursor</span>:<span class="value">pointer;</span></span> <span class="rule">}</span>  <span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript">$(<span class="string">"div"</span>).one(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span></span></code></div></div><div class="container"><div class="line"><code>      $(<span class="keyword">this</span>).height(<span class="number">30</span>)</code></div></div><div class="container"><div class="line"><code>             .css({cursor:<span class="string">"auto"</span>, backgroundColor:<span class="string">"green"</span>});</code></div></div><div class="container"><div class="line"><code>    });<span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article> </div>
</article>